import {ItemLineInfoDTO} from "@/api/afterSale/order/dto/ItemLineInfoDTO";
import {UnwrapNestedRefs} from "@vue/reactivity";
import {AfterSaleEnumConfigManager} from "@/manager/base/AfterSaleEnumManager";

/**
 *  自动查询管理器
 *
 * @author 徐寿春
 * 2023/4/17 18:09
 */
export function useAutocompleteQueryLineManager(afterSaleOrderConfig: UnwrapNestedRefs<AfterSaleEnumConfigManager>) {

    /**
     *
     *  自动查询后端物料对应数据
     *
     * @author 徐寿春
     * 2023/5/19 18:05
     */
    function querySearchAsync(queryString: string, cb: (arg: any) => void) {
        const results = queryString ?
            afterSaleOrderConfig.itemLineInfos.filter((item) => item.batch?.toLowerCase().indexOf(queryString.toLowerCase()) === 0) : afterSaleOrderConfig.itemLineInfos
        cb(results)
    }


    /**
     * 处理选择之后得到对数据映射关系
     *
     * @author 徐寿春
     * 2023/5/19 18:05
     */
    function handleSelect(row: ItemLineInfoDTO) {
        // 获取池中数据
        let filter = afterSaleOrderConfig.itemLineInfos.filter((item) => item.batch == row.batch)[0];
        row.itemCode = filter.itemCode
        row.itemName = filter.itemName

    }


    return {querySearchAsync, handleSelect}

}